<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** Member page class
*
* This is a member page class to view, add and  to delete from queue,  search and show the shipped list 
*
* @author  webMAPZ
* @version 39
* @copyright copyright (c) 2011, Boston Univeristy Under BSD
* @todo - add function to show the shipped list
* @todo - add link to logout
* @todo - check if the member is active
* @todo - add function to delete from queue
* @todo - add function to show the profile to edit and change membership
* 
*/

class Member extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}
	
	
	
/** member page
 * 
* this function to load the member page for users and show the queue, search
* @todo add a way to check the member if active
*/
	function index()
	{
            // wlcome page for the user with links
            
            // check what type customers (admin, empployee, regular)
            if ($this->session->userdata('c_logged_in') || $this->session->userdata('e_logged_in'))
		{
                    // get the customer id from the session cockie to be used as input to delete the movie or game to his/her 
                    $cus_id = $this->session->userdata('cus_id');
                    $emp_id = $this->session->userdata('emp_id');
                    // delete the dvd from the customer queue using cusrtomer id, film id or game id, dvd type and priority
                    if ($cus_id)
                    {
                        list ($user_type) = $this->model_user->check_user($cus_id);
                    }
                    else if ($emp_id)
                    {
                        list ($user_type) = $this->model_user->check_emp($emp_id);
                    }
                    else
                    {
                        redirect('yogette/error');
                    }

                    if ($user_type != FALSE)
                    {
                        if (is_null($user_type['access']) && $cus_id)
                        {
                            $member['query'] = $this->model_user->get_member_info($cus_id);
                            $this->load->view('view_welcome_member',$member);
                        }
                        else if($user_type['access'] == 'Employee' && $emp_id)
                        {
                            $empolyee['query'] = $this->model_user->get_employee_info($emp_id);
                            $this->load->view('view_welcome_employee',$empolyee);   
                        }
                        else if($user_type['access'] == 'Admin' && $emp_id)
                        { 
                            $admin['query'] = $this->model_user->get_admin_info($emp_id);
                            $this->load->view('view_welcome_admin',$admin);
                        }
                        else
                        {
                             redirect('yogette/error');
                        }
                        
                    }
                    else
                    { 
                       redirect('yogette/error');
                    }
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user');
		}
            
	}
	
        
        
        // this is the account page so the use can change update and modify his information
        function view_info()
        {
            if ($this->session->userdata('c_logged_in'))
            {
               //$member['query'] = $this->model_user->get_member_info($cus_id);
               //$this->load->view('view_edit_member_info',$member);
               redirect('yogette/user/update_user');
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user');
            }
        }
        
        
        function view_info_emp()
        {
            if ($this->session->userdata('e_logged_in'))
            {
               //$member['query'] = $this->model_user->get_member_info($cus_id);
               //$this->load->view('view_edit_member_info',$member);
               redirect('yogette/user/update_emp');
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user');
            }
        }
// show cart link
        function show_cart()
        {
               //echo"show cart link";
               redirect('yogette/cart');
        }
        
        
// show start shopping link
        function start_shopping()
        {
               //echo"start shopping link";
               redirect('yogette/menu');
               
        }
        
        function search() // search for menu zoms
	{
		// this rule to check the search field validaty as required 
		$this->form_validation->set_rules('search_field', 'Search field', 'required');
		
		// check the validation of the filled form  if it is false the user will be viewing  to search field again
		if ($this->form_validation->run() == FALSE)
		{
			//loading the view of search field 
			$this->load->view('view_search');
		}
		else
		{
			//processes their input login the user
			extract($_POST);
			
			// get the search array result from the seearch model from the DB by supplying the dvd type (games or movies) and the search field
			$search_results = $this->model_search->search($search_field);
			
			// run another function to generate the table and output to the member the list result by providing the search array result from the DB and and the dvd type either movie or game 
			$this->search_list($search_results);	
		}	
	}
	
        
/*	
	function search_list($search_results)
	{
		// check the dvd type either it is movies or games to generate the proper table 
		//if($dvd_type == 'movies')
		//{
			
			// assign to the search table the dvd type to be 1 "movie" so passing this variable will help to indicate the type of results either movies or games
			$search_table['dvd_type'] = 1;
			
			// check if the array of results is not empty before gemerating the table to be able to print the proper error
			//if it is empty will assign FALSE for both games and movies 
			if ($search_results != FALSE)
			{
				
				// clearing the table from any values to be sure that it is empty before adding the inputs
				$this->table->clear();
				
				// add Null to the empty culomns
				$this->table->set_empty("&nbsp;");
				
				// adding the heading of the table to be specific for movies
				$this->table->set_heading('PRODUCT ID', 'Product Name', 'Menu ID', 'Click to Add'); // zoms add to cart
				
				// run a for loop for each row in the result array to add the link to add a specific movie from the resulted array to the queue
				foreach ($search_results as $search_results)
				{
					
					//adding all required fields in addition to the add link to the queue
					$this->table->add_row($search_results['prod_id'], $search_results['p_name'], $search_results['menu_id'], anchor('member/add_queue/'.$search_results['prod_id']. '/' .$search_table['dvd_type'],'Add', array('class' => 'add')));
				}
				
				// generate the table with all required fields for movies
				$search_table['items'] = $this->table->generate();
				
				// assigning FALSE to games array to be sure not to print to the member and provide the proper error
				//$search_table['games'] = FALSE;
				
			}
			else
			{
				
				// assigning False to both movies and games arrays to provide the proper error
				$search_table['items'] = FALSE;
				//$search_table['games'] = FALSE;
				
			}
		
		// view the the both searching results tables for either movies or games by passing the generated search table arrays with the dvd type inside this array 
		$this->load->view('view_search_results',$search_table);
		
		// view the search field again after getting the results so the customer can search again if he/she does not want to add this movie or game to the queue
		$this->load->view('view_search');
		
	}*/
	
	
	
	
/** add a movie or a game to the customer view 
 * 
* this function to add game or movie to the customer queue
* @param int $dvd_id this is the specific film id or game id  to be added
* @param int $dvd_type this the type of the dvd either game or movie
* 
*/	
	/*function add_queue($prod_id)
	{
            echo "old add_queue";
	}*/
	
/** delete movie or a game from the queue
 * 
* this function to delete game or movie from the customer queue
* @param int $dvd_id this is the specific film id or game id  to be added
* @param int $dvd_type this the type of the dvd either game or movie
* @param int $priority this is the priority of the order list of the deleted movie or game
*/	
        
   /*     function update_queue()
        {
            //get the the customer id from the session cockie
            $cus_id = $this->session->userdata('cus_id');
                
            extract($_POST);
            
            $i = 1;
            foreach ( $item as $key => $value )
            {
                
               $this->model_menu->model_update_queue($cus_id, $key, $value); 
            }
            
            // go back to the index to view the member main page after deleting the movie to the queue
            $this->index();
        }*/
        
	
/** logout the customer
 * 
* this function to logout customer by destroying his/her session and cockie
* 
* 
* 
*/	
	function logout()
	{
		/* to destroy the seesion and logout*/
		$this->session->sess_destroy();
                redirect('yogette/user');
	}
}